﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameLib.WorldMap
{
	public class TileCollection<T> : ICollection<T>
	{
		public TileCollection(int width, int height)
		{
			Init(width, height);
		}

		#region ICollection<T> Members

		T[,] m_Tiles;

		public void Add(T item)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public void Init(int width, int height)
		{
			Width = width;
			Height = height;
			Clear();

		}

		public void Clear()
		{
			m_Tiles = new T[Height, Width];
		}

		public bool Contains(T item)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			m_Tiles.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get { return m_Tiles.Length; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(T item)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		#endregion

		#region IEnumerable<T> Members

		public IEnumerator<T> GetEnumerator()
		{
			foreach (T tile in m_Tiles)
				yield return tile;
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion

		public T this[int x, int y]
		{
			get
			{
				if (x < 0 || x >= Width || y < 0 || y >= Height)
					return default(T);

				return m_Tiles[y, x];
			}

			set
			{
				if (x >= 0 && x < Width && y >= 0 && y < Height)
					m_Tiles[y, x] = value;
			}
		}

		public int Width
		{
			get;
			private set;
		}

		public int Height
		{
			get;
			private set;
		}
	}
}
